//
//  MoodDataBaseTool.m
//  Today
//
//  Created by dllo on 15/5/28.
//  Copyright (c) 2015年 fanfan. All rights reserved.
//

#import "MoodDataBaseTool.h"

static sqlite3 *moodDB = nil;

@implementation MoodDataBaseTool

+ (void)openMoodDB
{
    if (moodDB) {
        return;
    }
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject ;
    NSLog(@"%@", NSHomeDirectory());
    path = [path stringByAppendingPathComponent:@"moods.rdb"];
    sqlite3_open(path.UTF8String, &moodDB);
    
}

+(void)closeMoodDB
{
    sqlite3_close(moodDB);
}
+ (void)createMoodDB
{
    [self openMoodDB];
    NSString *sqlCreate = [NSString stringWithFormat:@"create table mood(moodtext varchar(3000))"];
    sqlite3_exec(moodDB, sqlCreate.UTF8String, NULL, NULL, NULL);
    
}

+(void)insertMoodDB:(NSString *)str
{
    [self openMoodDB];
    NSString *sqlInsert = [NSString stringWithFormat:@"insert into mood values('%@')", str];
    sqlite3_exec(moodDB, sqlInsert.UTF8String, NULL, NULL, NULL);
}

+(void)deleteMoodDB:(NSString *)str
{
    [self openMoodDB];
    NSString *sqlDelete = [NSString stringWithFormat:@"delete from mood where moodText = '%@'", str];
    sqlite3_exec(moodDB, sqlDelete.UTF8String, NULL, NULL, NULL);
}

+(void)updateMoodDB:(NSString *)str1 : (NSString *)str2
{
    [self openMoodDB];
    NSString *sqlUpdate = [NSString stringWithFormat:@"update mood set moodText = '%@' where moodText = '%@'", str1, str2];
    sqlite3_exec(moodDB, sqlUpdate.UTF8String, NULL, NULL, NULL);
    
}

+(NSArray *)selectMoodDB
{
    [self openMoodDB];
    sqlite3_stmt *stmt = nil;
    NSString *sqlSelect = [NSString stringWithFormat:@"select * from mood"];
    int result = sqlite3_prepare_v2(moodDB, sqlSelect.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        NSMutableArray *array = [NSMutableArray array];
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 0);
            NSString *string = [NSString stringWithUTF8String:(const char *)title];
            
            [array addObject:string];
        }
        NSMutableArray *array1 = [NSMutableArray array];
        for (int i = 0; i < array.count; i++) {
            array1[i] = array[array.count - 1 - i];
        }
        sqlite3_finalize(stmt);
        
        return array1;
    }
    sqlite3_finalize(stmt);
    return [NSMutableArray array];
}


+ (void) openCollectDB
{
    if (moodDB) {
        return;
    }
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    path = [path stringByAppendingPathComponent:@"collect.rdb"];
    sqlite3_open(path.UTF8String, &moodDB);
    
}

+ (void) closeCollectDB
{
    sqlite3_close(moodDB);
}

+ (void) createCollectDB
{
    [self openCollectDB];
    NSString *sqlCreateCollect = [NSString stringWithFormat:@"create table collect(collectText varchar(3000))"];
    sqlite3_exec(moodDB, sqlCreateCollect.UTF8String, NULL, NULL, NULL);
    
}
+ (void) insertCollectDB:(NSString *)str
{
    [self openCollectDB];
    NSString *sqlInsertCollect = [NSString stringWithFormat:@"insert into collect values('%@')", str];
    sqlite3_exec(moodDB, sqlInsertCollect.UTF8String, NULL, NULL, NULL);
}
+ (void) deleteCollectDB:(NSString *)str
{
    [self openCollectDB];
    NSString *sqlDeleteCollect = [NSString stringWithFormat:@"delete from collect where collectText = '%@'", str];
    NSLog(@"%@", str);
    sqlite3_exec(moodDB, sqlDeleteCollect.UTF8String, NULL, NULL, NULL);
}
+ (NSArray *) selectCollectDB
{
    [self openCollectDB];
    sqlite3_stmt *stmt = nil;
    NSString *sqlSelectCollect = [NSString stringWithFormat:@"select * from collect"];
    int result = sqlite3_prepare_v2(moodDB, sqlSelectCollect.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        NSMutableArray *array = [NSMutableArray array];
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 0);
            NSString *string = [NSString stringWithUTF8String:(const char *)title];
            [array addObject: string];
        }
        NSMutableArray *array1 = [NSMutableArray array];
        for (int i = 0; i < array.count; i++) {
            array1[i] = array[array.count - 1 - i];
        }
        sqlite3_finalize(stmt);
        return array1;
    }
    sqlite3_finalize(stmt);
    return [NSMutableArray array];
}







@end
